package com.liveneo.adapter.mobile.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.liveneo.adapter.base.entity.PrpmLossItem;
import com.liveneo.adapter.base.mapper.PrpmLossItemMapper;
import com.liveneo.adapter.mobile.model.request.LossItemRequest;
import com.liveneo.adapter.mobile.service.LossItemService;
import com.liveneo.adapter.mobile.util.Constant;

@Service
public class LossItemServiceImpl implements LossItemService {
    @Autowired
    private PrpmLossItemMapper prpmLossItemMapper;

    // @Autowired
    // private PrpmLossItem78Mapper prpmLossItem78Mapper;
    @Override
    public List<PrpmLossItem> getLossItemByPhoneNo(String phoneNo) {
        return prpmLossItemMapper.selectByPhoneNo(phoneNo);
    }

    @Override
    public List<PrpmLossItem> getLossItemByCaseInfoId(String caseInfoId) {
        return prpmLossItemMapper.selectByCaseInfoId(caseInfoId);
    }

    @Override
    public List<PrpmLossItem> getLossItemByCaseNo(String caseNo) {
        return prpmLossItemMapper.selectByCaseNo(caseNo);
    }

    @Override
    public List<PrpmLossItem> getLossItemByRequest(LossItemRequest request) {
        return prpmLossItemMapper.selectByRequest(request);
    }

    @SuppressWarnings({ "unchecked", "rawtypes" })
    @Override
    public PrpmLossItem getLossItemByCaseNoLossId(String caseNo, String lossId) {
        Map params = new HashMap();
        params.put("caseNo", caseNo);
        params.put("lossId", lossId);
        return prpmLossItemMapper.selectByCaseNoLossId(params);
    }

    @Override
    public int deleteByCaseNo(String caseNo) {
        return prpmLossItemMapper.deleteByCaseNo(caseNo);
    }

    @Override
    public int saveList(List<PrpmLossItem> lossItemList) {
        return prpmLossItemMapper.insertList(lossItemList);
    }

    @Override
    public int updateLossItem(PrpmLossItem lossItem) {
        return prpmLossItemMapper.updateByPrimaryKeySelective(lossItem);
    }

    @Override
    public int save(PrpmLossItem lossItem) {
        return prpmLossItemMapper.insert(lossItem);
    }

    @Override
    public int updateLossItemByUuid(PrpmLossItem lossItem) {
        return prpmLossItemMapper.updateLossItemByUuid(lossItem);
    }

    @Override
    public List<PrpmLossItem> getLossItemByPhoneNoFlag(String phoneNo) {
        return prpmLossItemMapper.selectByPhoneNoFlag(phoneNo);
    }

    @Override
    public List<PrpmLossItem> getLossItemByCaseUuids(List<String> caseUuids) {
        return prpmLossItemMapper.getLossItemByCaseUuids(caseUuids);
    }

    @Override
    public List<PrpmLossItem> getLossItemByCaseUuidsAndLossId(String caseNo) {
        return prpmLossItemMapper.getLossItemByCaseUuidsAndLossId(caseNo);
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    @Override
    public List<PrpmLossItem> getLossItemByPhoneNoAndLossId(String phoneNo, String lossId) {
        Map params = new HashMap();
        params.put("mobile", phoneNo);
        params.put("lossId", lossId);
        return prpmLossItemMapper.getLossItemByPhoneNoAndLossId(params);
    }

    @Override
    public List<PrpmLossItem> getPendingList(int currentPage, int pagesize, String caseNo) {
        PageHelper.startPage(currentPage, pagesize);
        return prpmLossItemMapper.selectByBean(caseNo);
    }

    @Override
    public List<PrpmLossItem> getLossItemByUuids(List<String> uuids) {
        return prpmLossItemMapper.getLossItemByUuids(uuids);
    }

    // @Override
    @SuppressWarnings({ "rawtypes", "unchecked" })
    @Override
    public PrpmLossItem getLossItemByCaseNoLossIdUuid(String caseNo, String lossId, String lossUuid) {
        Map params = new HashMap();
        params.put("caseNo", caseNo);
        params.put("lossId", lossId);
        params.put("lossUuid", lossUuid);
        return prpmLossItemMapper.selectByCaseNoLossIdUuid(params);
    }

    @Override
    public List<PrpmLossItem> getLossItemByUserCode(String userType,String userId,String caseUuid) {
        Map<String,Object> params = new HashMap<String,Object>(); 
        
        params.put("caseUuid", caseUuid);
        if(StringUtils.endsWith("C", userType)){
        	params.put("mobile", userId);
        	return prpmLossItemMapper.getLossItemByPhoneNo(params);
        }else{
        	params.put("userCode", userId);
        	return prpmLossItemMapper.getLossItemByUserCode(params);
        }
    }
    
    @Override
    public List<String> getLossUuidByCaseNo(String caseNo) {
        return prpmLossItemMapper.getLossUuidByCaseNo(caseNo);
    }

	@Override
	public int updateLossStatusByUuidList(PrpmLossItem prpmLossItem) {
		return prpmLossItemMapper.updateLossStatusByUuidList(prpmLossItem);
	}

	/**
	 * userType:1-A（修理厂）;2-B（查勘端）；3-C（自助端）
	 */
	@Override
	public List<PrpmLossItem> getLossItemByCaseNo(String caseNo, String userType, String userCode) {
		Map<String,String> map = new HashMap<String,String>();
		map.put("caseNo", caseNo);
		if(StringUtils.endsWith(Constant.MOBILEUSER_CUSTOMER,userType)){
			map.put("mobile", userCode);
			return prpmLossItemMapper.selectByCaseNo4C(map);
		}else{
			map.put("surveyorCode", userCode);
			return prpmLossItemMapper.selectByCaseNo4AB(map);
		}
	}
	/**
	 * userType:1-A（修理厂）;2-B（查勘端）；3-C（自助端）
	 * 查询本人名下未完成的任务
	 */
	@Override
	public List<PrpmLossItem> getLossItemByCaseId(String caseInfoId, String userType, String userCode) {
		Map<String,String> map = new HashMap<String,String>();
		map.put("caseInfoId", caseInfoId);
		if(StringUtils.endsWith(Constant.MOBILEUSER_CUSTOMER,userType)){
			map.put("mobile", userCode);
			return prpmLossItemMapper.selectByCaseId4C(map);
		}else{
			map.put("surveyorCode", userCode);
			return prpmLossItemMapper.selectByCaseId4AB(map);
		}
	}

	@Override
	public List<PrpmLossItem> getLossItemByCaseUuidsAndUserId(String userType,String mobile,String caseUuid) {
		Map<String,String> map = new HashMap<String,String>();
		map.put("mobile", mobile);
		map.put("caseUuid", caseUuid);
		if(StringUtils.equals(userType, Constant.MOBILEUSER_CUSTOMER)){
			return prpmLossItemMapper.getLossItemByCaseUuidsAndPhoneNo(map);
		}else{
			return prpmLossItemMapper.getLossItemByCaseUuidsAndPhoneNo(map);
		}
		
	}

	/**
	  * @Title: updateLossItemStatusById
	  * @Description: 
	  * @param @param lossItem
	  * @param @return 设定文件
	  * @return 返回类型
	  * @throws
	  */
	@Override
	public int updateLossItemStatusById(PrpmLossItem lossItem) {
		return prpmLossItemMapper.updateByPrimaryKeySelective(lossItem);
	}

	@Override
	public int updateByPrimaryKeySelective(PrpmLossItem lossItem) {
		return prpmLossItemMapper.updateByPrimaryKeySelective(lossItem);
	}

	@Override
	public PrpmLossItem selectByPrimaryKey(String lossItemId) {
		return  prpmLossItemMapper.selectByPrimaryKey(lossItemId);
	}

    @Override
    public List<PrpmLossItem> selectLossItemByLossUuids(PrpmLossItem prpmLossItem) {
        
        return prpmLossItemMapper.selectLossItemByLossUuids(prpmLossItem);
    }
    
}
